package org.ronghao.androidwifiplatfrom.activity;

import java.util.List;

import org.ronghao.androidwifiplatfrom.Facade;
import org.ronghao.androidwifiplatfrom.R;
import org.ronghao.androidwifiplatfrom.entity.Game;
import org.ronghao.androidwifiplatfrom.utils.Contants;
import org.ronghao.androidwifiplatfrom.utils.MessageUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

//TODO:定时刷新
public class GameHallActivity extends Activity implements IUpdateData{
	private GridView mHallGridView;
	private List<Game> mGames;
	private GamesAdapter mGameAdapter;
	private Facade mFacade;
	private Button refreshButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gamehall);
		
		initData();
		initView();
	}

	private void initView() {
		mHallGridView = (GridView) findViewById(R.id.gamehall_gridview);
		mGameAdapter = new GamesAdapter(this, mGames);
		mHallGridView.setAdapter(mGameAdapter);
		
		refreshButton = (Button) findViewById(R.id.gamehall_button1);
		refreshButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				updateGames();
			}
		});
	}

	private void initData() {
		mFacade = new Facade(this);
		mGames = mFacade.getGames();
	}

	class GamesAdapter extends BaseAdapter {

		private Context mContext;
		private List<Game> mGames;

		public void setmGames(List<Game> mGames) {
			this.mGames = mGames;
		}

		public GamesAdapter(Context context, List<Game> games) {
			this.mContext = context;
			this.mGames = games;
		}

		@Override
		public int getCount() {
			return mGames == null ? 0 : mGames.size();
		}

		@Override
		public Object getItem(int position) {
			return mGames == null ? null : mGames.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = new GameItemView(mContext, mGames.get(position));
				convertView.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						mFacade.login((Integer) v.getTag());
						
						Intent intent = new Intent(GameHallActivity.this,
								GameWaitActivity.class);
						Bundle bundle = new Bundle();
//						String value = mGame.gameToString();
						//TODO:
						String value ="";
						value = MessageUtils.setData(mGames.get((Integer) v.getTag()));
						bundle.putString(Contants.COMMUNICATION_GAME, value);
						intent.putExtras(bundle);
						int j=0;
						do {
							try {
								Thread.sleep(500);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							j++;
						} while (!mFacade.getWifiState()&&j<80);
						if (j==80) {
							finish();
							System.exit(0);
						}
						startActivity(intent);
					}
				});
			}else{
				convertView = (GameItemView)convertView;
				((GameItemView) convertView).setmGame(mGames.get(position));
			}
			
			convertView.setTag(position);
			
			((GameItemView) convertView).refreshView();
			return convertView;
		}
		
		class GameItemView extends BaseView{
			
			private Game mGame;
			private TextView textview;
//			private ImageView bgImageView;

			public void setmGame(Game mGame) {
				this.mGame = mGame;
			}
			
			public GameItemView(Context context,Game game) {
				super(context);
				this.mGame = game;
				
				setContentView(R.layout.gamehall_gameitem);
			}

			@Override
			public void onLoad() {
				
			}

			@Override
			public void onRealse() {
				
			}

			@Override
			public void onDestory() {
				
			}

			@Override
			public void initView(View view) {
				textview = (TextView) view.findViewById(R.id.gamehall_gameitem_textView1);
//				bgImageView = (ImageView) view.findViewById(R.id.gamehall_gameitem_bg_imageview);
				
				textview.setText(mGame.getGameID()+mGame.getGameName()+mGame.getGameTotleNum());
			}
			
			public void refreshView() {
				textview.setText(mGame.getGameID()+mGame.getGameName()+mGame.getGameTotleNum());
			}
		}
	}
	
	/**
	 * 更新游戏列表
	 * @author ronghao
	 * @data 2012-11-23    下午6:50:05
	 */
	private void updateGames() {
		mGames = mFacade.getGames();
		mGameAdapter.setmGames(mGames);
		mGameAdapter.notifyDataSetChanged();
	}

	@Override
	public void setData(String msg) {
		
	}
}
